/** 
* @Title: ThreadScopeShareData.java 
* @Package com.lh.threadtest.t5 
* @Description: TODO
* @author Liu 
* @date 2018年1月16日 下午4:38:55 
* @version V1.0 
*/
package com.lh.threadtest.t5;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @ClassName: MyThreadScopeShareData
 * @Description: 线程范围内共享变量的概念与作用
 * 
 * 线程内共享，线程外独立，如数据库连接事务转账(各个操作线程相互独立，互不影响，各个模块操作数据各不同)
 * 线程内的共享变量及其作用
 * 
 * @author Liu
 * @date 2018年1月16日 下午4:38:55
 * 
 */
public class MyThreadScopeShareData {

	private static int data = 0;

	private static Map<Thread, Integer> dataMap = new HashMap<>();
	
	public static void main(String[] args) {
		for(int i = 0; i < 2; i++){
			new Thread(new Runnable() {
				
				public void run() {
					Integer data = new Random().nextInt();
					System.out.println(Thread.currentThread().getName()
							+ " has put data:" + data
							);
					dataMap.put(Thread.currentThread(), data);
					new A().get();
					new B().get();
					new C().get();
				}
			}).start();
		}
	}

	static class A {
		 void get(){
			Integer data = dataMap.get(Thread.currentThread());
			System.out.println("A from " + Thread.currentThread().getName()
					+ " get data:" + data
					);
		}
	}

	static class B {
		 void get(){
			Integer data = dataMap.get(Thread.currentThread());
			System.out.println("B from " + Thread.currentThread().getName()
					+ " get data:" + data
					);
		}
	}

	static class C {
		 void get(){
			Integer data = dataMap.get(Thread.currentThread());
			System.out.println("C from " + Thread.currentThread().getName()
					+ " get data:" + data
					);
		}
	}

}
